package cn.iocoder.yudao.module.ride.service.photo;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ride.controller.admin.photo.vo.*;
import cn.iocoder.yudao.module.ride.dal.dataobject.photo.PhotoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ride.dal.mysql.photo.PhotoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.ride.enums.ErrorCodeConstants.*;

/**
 * 证件照片 Service 实现类
 *
 * @author 小胡同学
 */
@Service
@Validated
public class PhotoServiceImpl implements PhotoService {

    @Resource
    private PhotoMapper photoMapper;

    @Override
    public Long createPhoto(PhotoSaveReqVO createReqVO) {
        // 插入
        PhotoDO photo = BeanUtils.toBean(createReqVO, PhotoDO.class);
        photoMapper.insert(photo);

        // 返回
        return photo.getId();
    }

    @Override
    public void updatePhoto(PhotoSaveReqVO updateReqVO) {
        // 校验存在
        validatePhotoExists(updateReqVO.getId());
        // 更新
        PhotoDO updateObj = BeanUtils.toBean(updateReqVO, PhotoDO.class);
        photoMapper.updateById(updateObj);
    }

    @Override
    public void deletePhoto(Long id) {
        // 校验存在
        validatePhotoExists(id);
        // 删除
        photoMapper.deleteById(id);
    }

    @Override
        public void deletePhotoListByIds(List<Long> ids) {
        // 删除
        photoMapper.deleteByIds(ids);
        }


    private void validatePhotoExists(Long id) {
        if (photoMapper.selectById(id) == null) {
            throw exception(PHOTO_NOT_EXISTS);
        }
    }

    @Override
    public PhotoDO getPhoto(Long id) {
        return photoMapper.selectById(id);
    }

    @Override
    public PageResult<PhotoDO> getPhotoPage(PhotoPageReqVO pageReqVO) {
        return photoMapper.selectPage(pageReqVO);
    }

}